<?php

function smarty_function_yuitree($params, &$smarty)
{

	$content = $params['Content'];
        $out = '';

        $out .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/yui/check/tree.css\">";
        $out .= "<script type=\"text/javascript\" src=\"../../build/treeview/treeview.js\" ></script>";

        $out .= "<script type=\"text/javascript\" src=\"../js/TaskNode.js\"></script>";

//  <div id=\"content\">
        $out .= "<form name=\"mainForm\" action=\"javscript:;\">
        <div class=\"newsItem\">
          <div id=\"expandcontractdiv\">
                <a href=\"javascript:tree.expandAll()\">Expand all</a>
                <a href=\"javascript:tree.collapseAll()\">Collapse all</a>"; 
//                $out .= "<a href=\"javascript:checkAll()\">Check all</a>
//                <a href=\"javascript:uncheckAll()\">Uncheck all</a>"
        $out .="</div>
          <div id=\"treeDiv1\"></div>

        </div>
        </form>
  </div>
        
      <div id=\"footerContainer\">
        <div id=\"footer\">
          <p>&nbsp;</p>
        </div>
      </div>
    </div>
  </div>";
//</div>";


        $out .= "<script type=\"text/javascript\">

        var tree;
        var nodes = [];
        var nodeIndex;
        
        function treeInit() {
                buildRandomTextNodeTree();
        // alert(tree);
        }
        
        function buildRandomTextNodeTree() {
                tree = new YAHOO.widget.TreeView(\"treeDiv1\");\n";

                foreach ($content as $item) {
                        if($item->parent == 0) {
                        		
                       		$checked = ($params['Data'][$item->id])?('true'):('false');
//                                print ($item->id." - ".$params['Data'][$item->id]."<br>");                        	
	
                                $out .= "\t\tvar tmpNode = new YAHOO.widget.TaskNode(\"$item->value\" , tree.getRoot(),false ,$checked,$item->id);\n";
//$out .= "\t\tvar tmpNode = new YAHOO.widget.TaskNode(\"$item->value\" , tree.getRoot(),false ,$checked);\n";
                        }
                }        
                

//                for (var i = 0; i < Math.floor((Math.random()*4) + 3); i++) {
//                        var tmpNode = new YAHOO.widget.TaskNode(\"task-\" + i, tree.getRoot(), false);
            //tmpNode.onCheckClick = onCheckClick;
                        //buildRandomTextBranch(tmpNode);
        
        $out .= "        //}
        
        tree.subscribe(\"checkClick\", onCheckClick);
        // tree.subscribe(\"labelClick\", onLabelClick);
                tree.draw();
        }

        var callback = null;

        function buildRandomTextBranch(node) {
                if (node.depth < 5) {
                        YAHOO.log(\"buildRandomTextBranch: \" + node.index);
                        for ( var i = 0; i < Math.floor(Math.random() * 4) ; i++ ) {
                                var tmpNode = new YAHOO.widget.TaskNode(node.label + \"-\" + i, node, false);
                // tmpNode.onCheckClick = onCheckClick;
                                buildRandomTextBranch(tmpNode);
                        }
                } else {
                    // tmpNode = new YAHOO.widget.TaskNode(node.label + \"-\" + i, node, false, true);
        }
        }


    function onCheckClick(node) {

        var location = \"admin.php5?go=17&action=setright&id=\" + node.index + \"&user=\" + document.Form.user.value;
        window.location.href = location;
        
       // return false;

//        YAHOO.log(node.label + \" check was clicked, new state: \" + 
//                node.checkState);
    }

    function checkAll() {
        var topNodes = tree.getRoot().children;
        for(var i=0; i<topNodes.length; ++i) {
            topNodes[i].check();
        }
    }

    function uncheckAll() {
        var topNodes = tree.getRoot().children;
        for(var i=0; i<topNodes.length; ++i) {
            topNodes[i].uncheck();
        }
    }

   function onLabelClick(node) {
       new YAHOO.widget.TaskNode(\"new\", node, false);
       node.refresh();
       return false;
   }




</script>";        


        return $out;
} 

/* vim: set expandtab: */

 
?>
